<?php

/* 视频举报分类 */
namespace app\admin\controller;

use app\admin\service\ReportClassifyService;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class VideoreportclassController extends AdminBaseController
{

    public function index()
    {
        
//        $list = Db::name('video_report_class')
//            ->order("list_order asc")
//            ->paginate(20);
        $params = $this->request->get();
        if (empty($params['langId'])) {
            $params['langId'] = $this->defaultLanguage['id'];
            $this->assign('langId', $this->defaultLanguage['id']);
        }else {
            $this->assign('langId', $params['langId']);
        }
        $params['type'] = 2;
        $list = ReportClassifyService::getData($params);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign('list', $list);
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }


    public function add()
    {
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            $data['type'] = 2;
            
            $name=$data['name'];
            
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $condition = [
                    'del_flag' => 0,
                    'name'=>$name,
                    'lang_id'=>$data['lang_id'],
                    'type' => 2
                ];
                $check = Db::name('user_report_classify')->where($condition)->find();
                if($check){
                    $this->error('名称已存在');
                }
            }
            

            $id = Db::name('user_report_classify')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('user_report_classify')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $this->assign('data', $data);
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            $classData = Db::name('user_report_classify')->where(['id' => $data['id']])->find();
            $id=$data['id'];
            $name=$data['name'];
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $condition = [
                    'del_flag' => 0,
                    'name'=>$name,
                    'type' => 2,
                    'lang_id' => $classData['lang_id'],
                ];
                $check = Db::name('user_report_classify')->where($condition)->where([['id','<>',$id]])->find();
                if($check){
                    $this->error('名称已存在');
                }
            }

            $rs = Db::name('user_report_classify')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }
    
    public function listOrder()
    {
        $model = Db::name('user_report_classify');
        parent::listOrders($model);
        $this->resetcache();
        $this->success("排序更新成功！");
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        $rs = Db::name('user_report_classify')->where("id={$id}")->update(['del_flag' => 1]);
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("videoreportclass/index"));
    }


    protected function resetcache(){

        $key='video_report';
        $level = DB::name('user_report_classify')
            ->where('type',2)
            ->field('id,name')
            ->order("list_order asc")
            ->select();

        if($level){
            setcaches($key,$level);
        }
    }
}